package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author tang
 * @data 2021/9/8
 * @apiNode
 */
@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 业务:文件上传
     * url:/file/upload
     * 请求类型:post
     * 参数:file
     * 返回值:SysResult(imageVO)
     * 高级API:MultipartFile  自动维护了缓存流/自动开关流
     */
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file){

        ImageVO imageVO = fileService.upload(file);
        if (imageVO == null){
            return SysResult.fail();
        }
        return SysResult.success(imageVO);
    }

    /**
     * 业务:实现图片的移除
     * url:/file/deleteFile
     * 请求方式:delete
     * 参数:virtualPath  文件虚拟路径
     * 返回值:SysResult
     */
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();

    }
}
